package com.zwz.controller;

import com.zwz.entity.RiskInfoMain;
import com.zwz.service.RiskInfoMainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/risk-info")
public class RiskInfoMainController {

    private final RiskInfoMainService service;

    @Autowired
    public RiskInfoMainController(RiskInfoMainService service) {
        this.service = service;
    }

    @PostMapping
    public ResponseEntity<?> create(@RequestBody RiskInfoMain riskInfo) {
        try {
            service.createRiskInfo(riskInfo);
            return ResponseEntity.status(HttpStatus.CREATED).body(riskInfo);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    @GetMapping("/{id}")
    public ResponseEntity<RiskInfoMain> getById(@PathVariable Integer id) {
        RiskInfoMain info = service.getRiskInfoById(id);
        return info != null ?
                ResponseEntity.ok(info) :
                ResponseEntity.notFound().build();
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<?> delete(@PathVariable Integer id) {
        try {
            service.deleteRiskInfo(id);
            return ResponseEntity.noContent().build();
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }
}